#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/8/22 17:58
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 中级-前K个高频元素.py
# @Software: PyCharm


class Solution:
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        m = {}
        for x in nums:
            if m.get(x):
                m[x] += 1
            else:
                m[x] = 1
        print(m)
        a = []
        b = {}
        for key in m:
            a.append(m[key])
            if b.get(m[key]):
                b[m[key]].append(key)
            else:
                b[m[key]] = [key]
        a.sort()
        print(b)
        print(a)
        ans = []
        while k:
            e = a.pop()
            if len(b[e]):
                ans = ans + [b[e].pop()]
                k -= 1
        return ans


def main():
    solve = Solution()
    nums = [3, 2, 3, 1, 2, 4, 5, 5, 6, 7, 7, 8, 2, 3, 1, 1, 1, 10, 11, 5, 6, 2, 4, 7, 8, 5, 6]
    k = 10
    print(nums, k)
    print(solve.topKFrequent(nums, k))


if __name__ == '__main__':
    main()
